﻿using AutoMapper;
using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.Data;

namespace R.FarmSmartSleep.RBAC.API.Apliction.Command.Roless
{
    /// <summary>
    /// 校色添加
    /// </summary>
    public class RolesCreateHandler:IRequestHandler<RolesCreateCommon,Result>
    {
        IBaseRepository<Roles> rolesRep; 
        IMapper mapper;
        public RolesCreateHandler(IBaseRepository<Roles> rolesRep, IBaseRepository<RolesPermissions> rolesPermissionsRep, IMapper _mapper)
        {
            this.rolesRep = rolesRep; 
            mapper = _mapper;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(RolesCreateCommon request, CancellationToken cancellationToken)
        {
            var Result = new Result() { Code = (int)CodeEnum.add };
            try
            {
                Roles roles = mapper.Map<Roles>(request);
                roles.CreateTime = DateTime.Now;
                var n = await rolesRep.AddAsync(roles);
                
                Result.Code = (int)CodeEnum.success;
                Result.Messege = "添加成功";
            }
            catch (Exception)
            {
                Result.Messege = "添加失败";
                throw;
            }
            return Result;
        }
    }
}
